package one.bugu.android.demon.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.common.util.PermissionManager;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import one.bugu.android.demon.MyApplication;
import one.bugu.android.demon.R;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.ui.fragment.FarmFragment;
import one.bugu.android.demon.ui.fragment.SnatchFragment;
import one.bugu.android.demon.ui.fragment.UserInfoFragment;
import one.bugu.android.demon.util.OnViewClickListener;
import one.bugu.android.demon.util.ShareImageUtils;
import one.bugu.android.demon.util.StatusBarUtils;

@LKContentView(R.layout.activity_warn)
public class WarnMainActivity extends MyBaseActivity {

    /**
     * 持仓预警
     */
    @LKInjectView(R.id.warn_cc)
    private LinearLayout warnCc;

    /**
     * 我的
     */
    @LKInjectView(R.id.warn_myInfo)
    private LinearLayout warnMyInfo;

    /**
     * 持仓预警图标
     */
    @LKInjectView(R.id.cc_icon)
    private ImageView ccIcon;

    /**
     * 持仓预警文字
     */
    @LKInjectView(R.id.cc_txt)
    private TextView ccTxt;

    /**
     * 我的图标
     */
    @LKInjectView(R.id.myInfo_icon)
    private ImageView myInfoIcon;

    /**
     * 我的文字
     */
    @LKInjectView(R.id.myInfo_txt)
    private TextView myInfoTxt;

    /**
     * 时间
     */
    private long mExitTime;

    /**
     * 布谷农场
     */
    @LKInjectView(R.id.warn_farm)
    private LinearLayout warnFarm;

    /**
     * 布谷农场图片
     */
    @LKInjectView(R.id.farm_icon)
    private ImageView farmIcon;

    /**
     * 布谷农场文字
     */
    @LKInjectView(R.id.farm_txt)
    private TextView farmTxt;

    /**
     * 预警预警信息的fragment
     */
    private SnatchFragment warnFragment;

    /**
     * 我的信息的fragment
     */
    private UserInfoFragment userInfoFragment;

    private FarmFragment farmFragment;

    private FragmentManager fragmentManager;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        int id = intent.getIntExtra(Constant.ID, 1);
        if (id == 1) {
            swichPage(1);
        } else if (id == 0) {
            swichPage(0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {

    }

    @Override
    protected void initView() {
        super.initView();
        //请求文件读写权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionManager.checkFilePermission(this);
        }

        //解决小米手机home键退出后重新再进入主页问题  避免从桌面启动程序后，会重新实例化入口类的activity
        if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }
        //设置状态栏
        StatusBarUtils.getInstance().setStatusBar(StatusBarUtils.SYSTEM_UI_TRANS,
                this, null, false);
        //取id值  为1 加载我的农场页面 否则加载预警主界面
        Intent intent = getIntent();
        int id = intent.getIntExtra(Constant.ID, 1);
        if (id == 1) {
            swichPage(1);
        } else {
            swichPage(0);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        //提前合成二维码
        ShareImageUtils.getInstance().compQRCode(this);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        //持仓预警点击事件 展示持仓预警界面 修改图标颜色和文字颜色
        warnCc.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                swichPage(0);
            }
        });

        //我的信息点击事件 修改图标颜色和文字颜色
        warnMyInfo.setOnClickListener(new OnViewClickListener() {
            @Override
            public void singleClick(View view) {
                swichPage(2);

            }
        });

        warnFarm.setOnClickListener(new OnViewClickListener() {
            @Override
            public void singleClick(View view) {
                swichPage(1);
            }
        });
    }

    private void swichPage(int index) {
        if (fragmentManager == null) {
            fragmentManager = getSupportFragmentManager();
        }
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (index) {
            case 0:
                if (warnFragment == null) {
                    warnFragment = new SnatchFragment();
                    fragmentTransaction.add(R.id.yj_content, warnFragment);
                } else {
                    fragmentTransaction.show(warnFragment);
                }
                ccIcon.setImageResource(R.mipmap.icon_tab_snatch_press);
                ccTxt.setTextColor(Color.parseColor("#5965B0"));
                myInfoIcon.setImageResource(R.mipmap.wode);
                myInfoTxt.setTextColor(Color.parseColor("#A2A3AB"));
                farmIcon.setImageResource(R.mipmap.my_nongchang);
                farmTxt.setTextColor(Color.parseColor("#A2A3AB"));
                break;
            case 1:
                if (farmFragment == null) {
                    farmFragment = new FarmFragment();
                    fragmentTransaction.add(R.id.yj_content, farmFragment);
                } else {
                    fragmentTransaction.show(farmFragment);
                }
                farmIcon.setImageResource(R.mipmap.nongchang_bugu);
                farmTxt.setTextColor(Color.parseColor("#5965B0"));
                ccIcon.setImageResource(R.mipmap.icon_tab_snatch_normal);
                ccTxt.setTextColor(Color.parseColor("#A2A3AB"));
                myInfoIcon.setImageResource(R.mipmap.wode);
                myInfoTxt.setTextColor(Color.parseColor("#A2A3AB"));
                break;
            case 2:
                if (userInfoFragment == null) {
                    userInfoFragment = new UserInfoFragment();
                    fragmentTransaction.add(R.id.yj_content, userInfoFragment);
                } else {
                    fragmentTransaction.show(userInfoFragment);
                }
                ccIcon.setImageResource(R.mipmap.icon_tab_snatch_normal);
                ccTxt.setTextColor(Color.parseColor("#A2A3AB"));
                myInfoIcon.setImageResource(R.mipmap.bottom_wode_icon);
                myInfoTxt.setTextColor(Color.parseColor("#5965B0"));
                farmIcon.setImageResource(R.mipmap.my_nongchang);
                farmTxt.setTextColor(Color.parseColor("#A2A3AB"));
                break;
        }
        fragmentTransaction.commit();
    }

    //隐藏fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (warnFragment != null) {
            fragmentTransaction.hide(warnFragment);
        }
        if (userInfoFragment != null) {
            fragmentTransaction.hide(userInfoFragment);
        }
        if (farmFragment != null) {
            fragmentTransaction.hide(farmFragment);
        }

    }

    //按两次手机返回键退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                MyApplication.getInstance().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
